package com.ruoyi.system.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.test.excel.EasyExcelTools;
import com.ruoyi.test.excel.ExcelDTO;
import com.ruoyi.test.geetest.GeetsSolveTools;
import com.ruoyi.test.selenium.AutoDealWork;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 自动化Controller
 */
@Controller
public class AutoController extends BaseController {
    private String prefix = "system/auto";
    public static String tmpExcel = "";
    public static List<ExcelDTO> WORK_DATA = new ArrayList<ExcelDTO>();

    @Value("${ruoyi.profile}")
    public String uploadPath;

    //@Autowired
    //private AutoDealWork work;
    @GetMapping("/index")
    public String toIndex()
    {
        return  "/login";
    }
    @GetMapping("/main")
    public String toMain()
    {
        return  "/main";
    }

    @GetMapping("/auto")
    public String auto()
    {
        return prefix + "/auto";
    }


    @GetMapping("/echarts")
    public String echarts() {

        return prefix + "/echarts";
    }


    @PostMapping("/system/auto/list")
    @ResponseBody
    public TableDataInfo list(String show) {
        startPage();
        List<ExcelDTO> list = new ArrayList<ExcelDTO>();
        /*ExcelDTO dto = new ExcelDTO();
        dto.setId(1);
        dto.setAremark("备注处理失败");*/
        if(WORK_DATA.size() > 0 && "YES".equalsIgnoreCase(show)){
            list.addAll(AutoDealWork.WORK_DATA_ERROR_RESULT);
            //先删除 再重新写
            //new File(tmpExcel).delete();
            //EasyExcel.write(tmpExcel, ExcelDTO.class).sheet("01handle").doWrite(WORK_DATA);

            // 这里 需要指定写用哪个class去写
            /*ExcelWriter excelWriter = EasyExcel.write(tmpExcel, ExcelDTO.class).build();
            WriteSheet writeSheet = EasyExcel.writerSheet("01handle").build();
            excelWriter.write(WORK_DATA, writeSheet);
            /// 千万别忘记finish 会帮忙关闭流
            excelWriter.finish();*/
        }

        return getDataTable(list);
    }

    /**
     * 下载处理结果数据
     */
    @PostMapping("/system/auto/download")
    @ResponseBody
    public AjaxResult download() {
        ExcelUtil<ExcelDTO> util = new ExcelUtil<ExcelDTO>(ExcelDTO.class);
        if(WORK_DATA.size() == 0){
            return AjaxResult.warn("暂无数据");
        }else{
            return util.exportExcel(WORK_DATA, "01handle");
        }

    }

    /**
     * 下载模板
     */
    @PostMapping("/system/auto/export")
    @ResponseBody
    public AjaxResult export() {
        List<ExcelDTO> list = new ArrayList<ExcelDTO>();
        ExcelUtil<ExcelDTO> util = new ExcelUtil<ExcelDTO>(ExcelDTO.class);
        return util.exportExcel(list, "01");
    }

    @PostMapping("/system/auto/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        //设置临时目录
        GeetsSolveTools.BASE_PATH = this.uploadPath;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String name = sdf.format(new Date());
        String path = GeetsSolveTools.BASE_PATH + "/"+name+ file.getOriginalFilename();
        Files.copy(file.getInputStream(),new File(path).toPath());
        tmpExcel = path;
        List<ExcelDTO> excelData = EasyExcelTools.parseExcel(tmpExcel);
        WORK_DATA = excelData;
        return AjaxResult.success(path);
    }

    @PostMapping("/system/auto/startnewpwd")
    @ResponseBody
    public AjaxResult startNewPwd(String msg){
        logger.info("==msg=={}",msg);
        if(CollectionUtils.isEmpty(WORK_DATA)){
            return AjaxResult.error("上传数据为0，不能开启辅助工具");
        }
        if(WORK_DATA.size() > 1000){
            return AjaxResult.error("上传数据大于1000，不能开启辅助工具");
        }
        AutoDealWork work = new AutoDealWork();
        work.startNewPwd(WORK_DATA);

        return AjaxResult.success("辅助人员录入");
    }


    @PostMapping("/system/auto/startreport")
    @ResponseBody
    public AjaxResult startReport(String msg){
        logger.info("==msg=={}",msg);
        if(CollectionUtils.isEmpty(WORK_DATA)){
            return AjaxResult.error("上传数据为0，不能开启辅助工具");
        }
        if(WORK_DATA.size() > 1000){
            return AjaxResult.error("上传数据大于1000，不能开启辅助工具");
        }
        AutoDealWork work = new AutoDealWork();
        work.writeReport(WORK_DATA);

        return AjaxResult.success();
    }
}
